﻿using Architecture.Domain.Dto.UserInfoRoleDto;
using Architecture.Repository.Interface;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Domain.Service.UserInfoRoleServices
{
    public class UserInfoRoleService : IUserInfoRoleService
    {
        /// <summary>
        ///  用户信息角色服务
        /// </summary>
        protected readonly IUserInfoRoleRepository _userInfoRoleRepository;

        /// <summary>
        /// 用户信息仓储
        /// </summary>
        protected readonly IUserInfoRepository _userInfoRepository;

        /// <summary>
        /// 角色仓储
        /// </summary>
        protected readonly IRoleRepository _roleRepository;

        /// <summary>
        ///  构造函数
        /// </summary>
        /// <param name="userInfoRoleRepository">用户信息角色服务</param>
        /// <param name="userInfoRepository">用户信息仓储</param>
        /// <param name="roleRepository">角色仓储</param>
        public UserInfoRoleService(IUserInfoRoleRepository userInfoRoleRepository, IUserInfoRepository userInfoRepository, IRoleRepository roleRepository)
        {
            _userInfoRoleRepository = userInfoRoleRepository;
            _userInfoRepository = userInfoRepository;
            _roleRepository = roleRepository;
        }

        /// <summary>
        /// 添加用户角色
        /// </summary>
        /// <param name="ids">用户ID集合</param>
        /// <param name="roleId">角色ID</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> AddUserInfoRole(AddUserInfoRole addUserInfoRole)
        {
            
            var userInfoRole = new UserInfoRole();
            int key = 0;
            foreach (var id in addUserInfoRole.ids)
            {
                if (id != 0)
                {
                    var result = await _userInfoRoleRepository.GetAsync(x=>x.UserId == id && x.RoleId == addUserInfoRole.roleId);
                    if (result.Count == 0)
                    {
                        userInfoRole.UserInfoRoleId =0;
                        userInfoRole.UserId = id;
                        userInfoRole.RoleId = addUserInfoRole.roleId;
                        key+=await _userInfoRoleRepository.AddAsync(userInfoRole);
                    }
                }
            }
            return key;
        }

        /// <summary>
        /// 删除用户角色
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="userId">用户ID</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> DeletedUserInfoRole(int roleId, int userId)
        {
            var res = _userInfoRoleRepository.GetAsync(x => x.RoleId == roleId && x.UserId == userId);
            if (res != null)
            {
                foreach (var item in await res)
                {
                    item.IsDeleted = true;
                    return await _userInfoRoleRepository.UpdateAsync(item);
                }
            }
            return -1;
        }

        /// <summary>
        /// 通过角色ID用户表中的部门ID
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>返回部门ID集合</returns>
        public async Task<List<UserInfoRoleDepartmentDto>> GetDepartmentByRoleId(int roleId)
        {
            var list = from a in await _roleRepository.GetAllAsync()
                       join b in await _userInfoRoleRepository.GetAllAsync()
                       on a.RoleId equals b.RoleId
                       join c in await _userInfoRepository.GetAllAsync() on b.UserId equals c.UserId
                       where a.RoleId == roleId
                       select new UserInfoRoleDepartmentDto
                       {
                           id=c.DepartmentId
                       };
            return list.Distinct().ToList();
        }

        /// <summary>
        /// 通过角色ID查询用户表中的用户ID
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>返回用户ID和用户名集合</returns>
        public async Task<object> GetUserInfoByRoleId(int roleId)
        {
            var list = from a in await _userInfoRepository.GetAllAsync()
                       join b in await _userInfoRoleRepository.GetAllAsync()
                       on a.UserId equals b.UserId
                       join c in await _roleRepository.GetAllAsync() on b.RoleId equals c.RoleId
                       where c.RoleId == roleId
                       select new
                       {
                           id = a.UserId,
                       };
            return list.Distinct().ToList();
        }
    }
}
